package top.infopub.job.fia.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.job.bpa.service.ContractExtService;
import top.infopub.job.cap.service.LoanFinService;
import top.infopub.job.fia.dao.FeeNoteDao;
import top.infopub.job.fia.service.FeeNoteService;
import top.infopub.job.fia.service.PayBillService;
import top.infopub.model.fia.FeeNote;

import java.math.BigDecimal;
import java.util.Map;


@Service
public class FeeNoteServiceImpl extends GenericServiceImpl<FeeNote, String> implements FeeNoteService {

    @Autowired
    private FeeNoteDao feeNoteDao;

    @Autowired
    private LoanFinService loanFinService;

    @Autowired
    private PayBillService payBillService;

    @Autowired
    private ContractExtService contractExtService;

    @Override
    public GenericDao<FeeNote, String> getDao() {
        return feeNoteDao;
    }

//    @Override
//    public BigDecimal selectProQaOfFinSpread(Date dateStart, Date dateEnd, String hrOrgProId,
//                                             String proCode) {
//        //（财务-【付款单登记表】支出类型-建设期利息{实付金额}+财务-【付款单登记表】支出类型-融资费用{实付金额}+财务-【付款单登记表】支出类型-融资担保费{实付金额}）/【融资放款单】{加权平均综合成本}*（商务-【承继协议】{建设期融资利率}—融资-【融资放款单】{加权平均综合成本}）（*统计时间：付款日期，原始单位：元）
//        //【承继协议】{建设期融资利率}：取值项目最新版本【承继协议】
//        //【融资放款-加权平均综合成本】：截止统计月份末，该项目所有【融资放款】中{综合成本}加权平均值：合计（综合成本*放款明细总额）/合计（放款明细总额）
//        Map<String, Object> wherePay = Maps.newHashMap();
//        wherePay.put("payDateStart", dateStart);
//        wherePay.put("payDateEnd", dateEnd);
//        wherePay.put("hrOrgProId", hrOrgProId);
//        // 财务-【付款单登记表】支出类型-建设期利息{实付金额}+财务-【付款单登记表】支出类型-融资费用{实付金额}+财务-【付款单登记表】支出类型-融资担保费{实付金额}
//        BigDecimal sumAct = payBillService.selectProQaOfFinSpread(wherePay);
//        // 【融资放款】中，合计（综合成本*放款明细总额）
//        Map<String, Object> loanFinMultiSumWhere = Maps.newHashMap();
//        loanFinMultiSumWhere.put("loanDateEnd", dateEnd);
//        loanFinMultiSumWhere.put("proCode", proCode);
//        BigDecimal loanFinMultiSum = loanFinService.selectProQaOfFinSpreadMultiSum(loanFinMultiSumWhere);
//        // 【融资放款】中，合计（放款明细总额）
//        Map<String, Object> loanFinSumWhere = Maps.newHashMap();
//        loanFinSumWhere.put("loanDateEnd", dateEnd);
//        loanFinSumWhere.put("proCode", proCode);
//        BigDecimal loanFinSum = loanFinService.selectProQaOfFinSpreadSum(loanFinSumWhere);
//        // 【融资放款-加权平均综合成本】 保留四位小数
//        BigDecimal avg = null;
//        if (loanFinSum.compareTo(BigDecimal.ZERO) == 0) {
//            avg = BigDecimal.ZERO;
//        }
//        else {
//            avg = loanFinMultiSum.divide(loanFinSum, 4, BigDecimal.ROUND_HALF_UP);
//        }
//        // {建设期融资利率}：取值项目最新版本【承继协议】
//        Map<String, Object> contractExtWhere = Maps.newHashMap();
//        contractExtWhere.put("proCode", proCode);
//        contractExtWhere.put("processStatus", 9);
//        contractExtWhere.put("orderCondition", " version_no desc ");
//        List<ContractExt> contractExtList = contractExtService.selectByMapWhere(contractExtWhere);
//        // 需要除以100，折算成小数
//        BigDecimal buildCapRate = contractExtList.get(0).getBuildCapRate().divide(
//            ParserUtil.HUNDRED);
//        // 根据公式计算
//        if (avg.compareTo(BigDecimal.ZERO) == 0) {
//            return BigDecimal.ZERO;
//        }
//        else {
//            return (sumAct.divide(avg, 4, BigDecimal.ROUND_HALF_UP).multiply(buildCapRate.subtract(avg))).setScale(
//                2, BigDecimal.ROUND_HALF_UP);
//        }
//    }

    @Override
    public BigDecimal selectProQaOfManageFee(Map<String, Object> where) {
        return feeNoteDao.selectProQaOfManageFee(where);
    }

    @Override
    public BigDecimal selectProQaOfFiaFee(Map<String, Object> where) {
        return feeNoteDao.selectProQaOfFiaFee(where);
    }

    @Override
    public BigDecimal selectProQaOfManageFeeOne(Map<String, Object> where) {
        return feeNoteDao.selectProQaOfManageFeeOne(where);
    }

}